package modelo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
public class Ingrediente extends ConexionBD implements ProductoIngredente{

    private int codigo;
    private float stock;

    public Ingrediente() {
        super();
    }

    public boolean registrar(){
        abrirConexion();
        boolean sw = false;
        Vector<String> v1= new Vector<String>();
    	Vector v2= new Vector();
    	v1.add("codigo");
    	v1.add("descripcion");
    	v1.add("stock");
    	v1.add("estatus");
    	v2.add(getCodigo());
    	v2.add(getDescripcion());
    	v2.add(getStock());
    	v2.add(getEstatus());
        String tira=facade.generar(v1, v2, "Ingrediente");
       try {
            Statement stam = conexion.createStatement();
            stam.execute(tira);
            sw= true;
        } catch (SQLException ex) {
                   Logger.getLogger(Ingrediente.class.getName()).log(Level.SEVERE, null, ex);
     
             sw= false;
        }
        
        cerrarConexion();
        return sw;
    }

  public boolean actualizar(){
     abrirConexion();
     boolean sw = false;
     String tira="UPDATE Ingrediente SET stock ="+getStock()+" WHERE codigo="+getCodigo()+"  AND estatus = 'A'";
     try {
          Statement stam = conexion.createStatement();
            stam.execute(tira);
            sw= true;
     } catch (SQLException ex) {
            Logger.getLogger(Ingrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     cerrarConexion();
     return sw;
 }

  public boolean  consultar(){
    abrirConexion();
    boolean sw = false;
    String tira="SELECT * FROM Ingrediente WHERE codigo="+getCodigo()+" AND estatus='A'";
    try {
         Statement stam = conexion.createStatement();
            ResultSet rs =  stam.executeQuery(tira);
        if (rs.next()){
            sw = true;
            setDescripcion(rs.getString("descripcion"));
            setCodigo(rs.getInt("codigo"));
            setStatus(rs.getString("estatus"));
            setStock(rs.getFloat("stock"));
         }
    } catch (SQLException ex) {
        Logger.getLogger(Ingrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sw;
  }

  public LinkedList<Ingrediente> consultarGeneral(){
    LinkedList<Ingrediente> v = new LinkedList<Ingrediente>();
    String tira="SELECT * FROM Ingrediente WHERE estatus='A'";
    abrirConexion();
    try {
        Statement stam = conexion.createStatement();
        ResultSet rs =  stam.executeQuery(tira);
        while (rs.next()){
            Ingrediente  i = new Ingrediente();
            i.setDescripcion(rs.getString("descripcion"));
            i.setCodigo(rs.getInt("codigo"));
            i.setStatus(rs.getString("estatus"));
            i.setStock(rs.getFloat("stock"));
            v.add(i);
         }
    } catch (SQLException ex) {
        Logger.getLogger(Ingrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    cerrarConexion();
    return v;
  }
  

  public void restarStock(){
       abrirConexion();
       String tira="UPDATE Ingrediente SET stock ="+getStock()+" WHERE codigo="+getCodigo()+"  AND estatus = 'A'";
       try {
           Statement stam = conexion.createStatement();
           stam.execute(tira);
       } catch (SQLException ex) {
           Logger.getLogger(Ingrediente.class.getName()).log(Level.SEVERE, null, ex);
       }
       cerrarConexion();
  }

  public LinkedList<String []> ingredientesMasUsados(boolean asc, boolean desc){
    LinkedList<String []> v = new LinkedList<String []>();
    String tira="SELECT raciones , descripcion , codigo FROM  Ingrediente,(SELECT SUM(racion) AS raciones,codigoIngrediente AS cod FROM IngredientePorProducto  GROUP BY codigoIngrediente) Suma WHERE Ingrediente.codigo=Suma.cod ORDER BY raciones";
    if(asc)
        tira+= "ASC";
    if(desc)
        tira+= "DESC";
    abrirConexion();
    try {
        Statement stam = conexion.createStatement();
        ResultSet rs =        stam.executeQuery(tira);

        while (rs.next()){
            String [] s = {rs.getString("descripcion"), ""+rs.getFloat("cantidades"), ""+rs.getInt("id")};
            v.add(s);
        }
  } catch (SQLException ex) {
        Logger.getLogger(Producto.class.getName()).log(Level.SEVERE, null, ex);
    }
    return v;
  }
/* * public class coleccion{
	  private LinkedList v = new LinkedList();
	  public coleccion(){
		  for(int i=1; i<v.size(); i++){
			v.add(v.getString(i)("descripcion"));
			v.add(v.getint(codigo));
		  }
	}
 
  }
  public ListIterator miiterator(int index){
	  return v.listIterator(index);
  }
  */
  public boolean revisarDisponibilidadStock(float porcion){
      consultar();
      if((getStock()-porcion)>=0)
          return true;
      else
          return false;
  }
  
  
    public float getStock() {
        return stock;
    }

    public void setStock(float stock) {
        this.stock = stock;
    }

    public String getStatus() {
        return estatus;
    }

    public void setStatus(String estatus) {
        this.estatus = estatus;
    }


}
